#include "Modbus_ext.h"

uint16_t pu16ModbusGetInfoTableIndex(uint16_t mBAddr)
{
    int left = 0;
    int right = MODBUS_REG_NUM - 1;
    
    while (left <= right) 
    {
        int mid = left + (right - left) / 2; // 防止整数溢出的写法
        
        if (ModbusInfoTable[mid].modbusAddr == mBAddr) {
            return mid; // 找到目标，返回索引
        } else if (ModbusInfoTable[mid].modbusAddr < mBAddr) {
            left = mid + 1; // 目标在右半部分
        } else {
            right = mid - 1; // 目标在左半部分
        }
    }
    
    return MODBUS_REG_NUM; // 未找到
}
